<?php
require_once("libs/dbmodel.php");

class Cotizacion extends DbModel {

	/**
	 * Get the received price list for a purchaser
	 * @param $id_purchaser
	 * @param $id_request
	 * @param $price_min
	 * @param $price_max
	 * @param $locations
	 * @param $with_photo
	 * @return boolean
	 */
	public function getReceivedPriceList($id_purchaser, $id_request, $price_min, $price_max, $locations, $with_photo) {
		$sql = "SELECT 
				    c.id,
				    a.descripcion, 
				    c.precio, 
				    u.nombre, 
				    u.id_ubicacion, 
				    u.tipo_cuenta, 
				    (
				       SELECT COUNT(*)
				       FROM fotos_cotizacion f
				       WHERE f.id_cotizacion = c.id
				    ) as fotos
				FROM cotizacion c, usuario u, articulo a
				WHERE c.id_comprador = $id_purchaser
				AND c.id_vendedor = u.id
				AND c.id_articulo = a.id
				AND c.estado = 'P'
				";
		
		if ($id_request != "*") {
			$sql .= "AND c.id_articulo = $id_request
					"; 
		}
		
		if ($price_min != "*") {
			$sql .= "AND c.precio >= $price_min
					";
		}
		
		if ($price_max != "*") {
			$sql .= "AND c.precio <= $price_max
					";
		}
		
		if ($locations != "*") {
			$sql .= "AND u.id_ubicacion IN ($locations)
					"; 
		}
		
		if ($with_photo != "*") {
			$sql .= "AND (
					   SELECT COUNT(*)
					   FROM fotos_cotizacion f
					   WHERE f.id_cotizacion = c.id
					) ";
			
			if ($with_photo == "S") {
				$sql .= "> 0";
			} else {
				$sql .= "= 0";
			}
		}
		
		if ($this->execQuery($sql)) {
			return true;
		} else {
			return false;
		}
	}
	
}

?>
